package plus.chendd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LinuxMemoryInfo {
  public static void main(String[] args) {
    try {
      Process process = Runtime.getRuntime().exec("free -m");
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        if (line.startsWith("Mem:")) {
          String[] parts = line.split("\\s+");
          long totalMemory = Long.parseLong(parts[1]) * 1024; // 将单位转换为字节
          long usedMemory = Long.parseLong(parts[2]) * 1024;
          long freeMemory = Long.parseLong(parts[3]) * 1024;
          long availableMemory = Long.parseLong(parts[6]) * 1024;
          
          System.out.println("Total Memory: " + totalMemory + " bytes");
          System.out.println("Used Memory: " + usedMemory + " bytes");
          System.out.println("Free Memory: " + freeMemory + " bytes");
          System.out.println("Available Memory: " + availableMemory + " bytes");
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
